package com.erpanderp.wam.usm.view.leave;

import java.text.DateFormatSymbols;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import org.primefaces.model.map.MapModel;

import com.erpanderp.wam.adm.model.entity.Module;
import com.erpanderp.wam.adm.model.entity.SAPERPModule;
import com.erpanderp.wam.adm.model.service.ModuleDAO;
import com.erpanderp.wam.adm.model.service.SAPERPModuleDAO;
import com.erpanderp.wam.usm.model.entity.leave.EmployeeAttendance;
import com.erpanderp.wam.usm.model.entity.timesheet.Timesheet;
import com.erpanderp.wam.usm.model.entity.timesheet.TimesheetDetail;
import com.erpanderp.wam.usm.model.entity.user.Employee;
import com.erpanderp.wam.usm.model.service.timesheet.TimesheetDAO;
import com.erpanderp.wam.usm.model.service.timesheet.TimesheetDetailDAO;
import com.erpanderp.wam.usm.model.service.user.EmployeeDAO;
import com.erpanderp.wam.util.CommonLookup;
import com.erpanderp.wam.util.WAMUtil;

@ManagedBean
@ViewScoped
public class AttendanceBean {

	private Map<String, Integer> monthsMap = CommonLookup.getMonths();
	private Map<String, Integer> yearsMap = WAMUtil.getLastSixYears();
	private Map<String, String> modulesMap = new HashMap<String, String>();
	private Integer selectedMonth;
	private Integer selectedYear;
	private String selectedModule;
	
	private List<EmployeeAttendance> employeeAttendances = new ArrayList<EmployeeAttendance>();
	String monthNames[] = new DateFormatSymbols().getMonths();
	private boolean RENDERTABLE;
	private Integer NO_OF_DAYS;
	SAPERPModuleDAO saperpModuleDAO = new SAPERPModuleDAO();

	public AttendanceBean() {
		Calendar calendar = Calendar.getInstance();
		selectedMonth = calendar.get(Calendar.MONTH);
		selectedYear = calendar.get(Calendar.YEAR);
		List<SAPERPModule> tempList = saperpModuleDAO.getSAPERPModules();
		for(SAPERPModule module : tempList){
			modulesMap.put(module.getDisplayName(), module.getDisplayName());			
		}
	}

	public void searchActionListener() {
		employeeAttendances.clear();
		RENDERTABLE = true;
		EmployeeDAO employeeDAO = new EmployeeDAO();
		List<Employee> employees = employeeDAO.getEmployees(selectedModule, null, null, null);

		
		TimesheetDetailDAO timesheetDetailDAO = new TimesheetDetailDAO();
		Calendar calendar1 = new GregorianCalendar(selectedYear,
				selectedMonth, 1);
		NO_OF_DAYS = calendar1.getActualMaximum(Calendar.DAY_OF_MONTH);
		System.out.println(NO_OF_DAYS);

		Collections.sort(employees);
		
		for (Employee employee : employees) {
			EmployeeAttendance attendance = new EmployeeAttendance();	
			Calendar calendar2 = new GregorianCalendar(selectedYear,
					selectedMonth, NO_OF_DAYS);
			attendance.setEmployee(employee);

			List<TimesheetDetail> timesheetDetails = timesheetDetailDAO
					.getTimesheetDetails(calendar1.getTime(),
							calendar2.getTime(), employee.getEmployeeId());

			if (timesheetDetails != null) {
				Collections.sort(timesheetDetails);

				SimpleDateFormat dateFormat = new SimpleDateFormat("MMM-dd");
				for (TimesheetDetail detail : timesheetDetails) {
					attendance.getAttendances().add(detail.getTimesheetDetailStatus() == null
									|| detail.getTimesheetDetailStatus().equals("") ? "P"
									: detail.getTimesheetDetailStatus());
					String day = dateFormat.format(detail.getTimesheetDate());
					attendance.getDays().add(day);
					if(detail.getTimesheetDetailStatus() != null && !detail.getTimesheetDetailStatus().equals("")){
						attendance.getStyles().add("color: green;");
					}else{
						attendance.getStyles().add("");
					}
				}
				employeeAttendances.add(attendance);

			}
			
		}
	}

	public Map<String, Integer> getMonthsMap() {
		return monthsMap;
	}

	public void setMonthsMap(Map<String, Integer> monthsMap) {
		this.monthsMap = monthsMap;
	}

	public Map<String, Integer> getYearsMap() {
		return yearsMap;
	}

	public void setYearsMap(Map<String, Integer> yearsMap) {
		this.yearsMap = yearsMap;
	}

	public Integer getSelectedMonth() {
		return selectedMonth;
	}

	public void setSelectedMonth(Integer selectedMonth) {
		this.selectedMonth = selectedMonth;
	}

	public Integer getSelectedYear() {
		return selectedYear;
	}

	public void setSelectedYear(Integer selectedYear) {
		this.selectedYear = selectedYear;
	}

	public List<EmployeeAttendance> getEmployeeAttendances() {
		return employeeAttendances;
	}

	public void setEmployeeAttendances(
			List<EmployeeAttendance> employeeAttendances) {
		this.employeeAttendances = employeeAttendances;
	}

	public boolean isRENDERTABLE() {
		return RENDERTABLE;
	}

	public void setRENDERTABLE(boolean rENDERTABLE) {
		RENDERTABLE = rENDERTABLE;
	}

	public Integer getNO_OF_DAYS() {
		return NO_OF_DAYS;
	}

	public void setNO_OF_DAYS(Integer nO_OF_DAYS) {
		NO_OF_DAYS = nO_OF_DAYS;
	}

	public String getSelectedModule() {
		return selectedModule;
	}

	public void setSelectedModule(String selectedModule) {
		this.selectedModule = selectedModule;
	}

	public Map<String, String> getModulesMap() {
		return modulesMap;
	}

	public void setModulesMap(Map<String, String> modulesMap) {
		this.modulesMap = modulesMap;
	}
}
